package cn.white.ymc.mvvmdemo.data

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import cn.white.ymc.mvvmmaster.R
import cn.white.ymc.mvvmmaster.data.UserInfoViewModel
import cn.white.ymc.mvvmmaster.data.bean.UserInfo
import cn.white.ymc.mvvmmaster.data.local.db.DBHelper
import kotlinx.android.synthetic.main.activity_git_hub.*

/**
 *  github 请求api
 *  网络请求使用 案例
 *
 *  @data 2018年11月6日 10:00:26
 */

class GitHubActivity : AppCompatActivity() {
    private lateinit var infoViewModel : UserInfoViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_git_hub)

        DBHelper.getInstance().initDB(this)

        infoViewModel = ViewModelProviders.of(this).get(UserInfoViewModel::class.java)
        infoViewModel.init(this)
        infoViewModel.getUser("yangmingchuan").observe(this,
                Observer<UserInfo> { user -> updateUserInfo(user)} )
    }

    private fun updateUserInfo(user: UserInfo?) {
        Log.e("GitHubActivity",user.toString())
        tv_git_id.text = user!!.id.toString()
        tv_git_name.text = user.name

    }

}
